﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using HML.Design;

namespace HML
{
    /// <summary>
    /// TabItem自定义按钮
    /// </summary>
    [TypeConverter(typeof(PropertyOrderConverter))]
    public class TabControlPlusTabItemCustomButton
    {
        #region 新增事件

        public delegate void TabItemCustomButtonEventHandler(object sender, TabControlPlusTabItemCustomButttonOperatedEventArgs e);
        private event TabItemCustomButtonEventHandler tabItemCustomButtonClick;
        /// <summary>
        /// TabItem自定义按钮单击事件
        /// </summary>
        [Description("TabItem自定义按钮单击事件")]
        [Category("杂项_选项自定义按钮")]
        public event TabItemCustomButtonEventHandler TabItemCustomButtonClick
        {
            add { this.tabItemCustomButtonClick += value; }
            remove { this.tabItemCustomButtonClick -= value; }
        }

        private event TabItemCustomButtonEventHandler tabItemCustomButtonCheckedChanged;
        /// <summary>
        /// TabItem自定义按钮checked更改事件
        /// </summary>
        [Description("TabItem自定义按钮checked更改事件")]
        [Category("杂项_选项自定义按钮")]
        public event TabItemCustomButtonEventHandler TabItemCustomButtonCheckedChanged
        {
            add { this.tabItemCustomButtonCheckedChanged += value; }
            remove { this.tabItemCustomButtonCheckedChanged -= value; }
        }

        public delegate void TabItemCusomButtonDrawAfterEventHandler(object sender, TabControlPlusTabItemCustomButtonDrawAfterEventArgs e);
        private event TabItemCusomButtonDrawAfterEventHandler tabItemCusomButtonDrawAfter;
        /// <summary>
        /// TabItem自定义按钮绘制后事件
        /// </summary>
        [Description("TabItem自定义按钮绘制后事件")]
        [Category("杂项_选项自定义按钮")]
        public event TabItemCusomButtonDrawAfterEventHandler TabItemCusomButtonDrawAfter
        {
            add { this.tabItemCusomButtonDrawAfter += value; }
            remove { this.tabItemCusomButtonDrawAfter -= value; }
        }

        #endregion

        #region 属性

        private string key = "";
        /// <summary>
        /// 按钮Key（必须唯一，可用于索引）
        /// </summary>
        [Description("按钮Key（必须唯一，可用于索引）")]
        [Category("杂项")]
        [PropertyOrder(-138)]
        [DefaultValue("")]
        public string Key
        {
            get { return this.key; }
            set
            {
                if (this.key == value)
                    return;

                this.key = value;
            }
        }

        private bool visible = true;
        /// <summary>
        /// 按钮是否显示
        /// </summary>
        [Description("按钮是否显示")]
        [Category("杂项")]
        [PropertyOrder(-136)]
        [DefaultValue(true)]
        public bool Visible
        {
            get { return this.visible; }
            set
            {
                if (this.visible == value)
                    return;

                this.visible = value;
                this.InitializeInvalidate();
            }
        }

        private bool enabled = true;
        /// <summary>
        /// 按钮使用状态
        /// </summary>
        [Description("按钮使用状态")]
        [Category("杂项")]
        [PropertyOrder(-134)]
        [DefaultValue(true)]
        public bool Enabled
        {
            get { return this.enabled; }
            set
            {
                if (this.enabled == value)
                    return;

                this.enabled = value;
                this.Invalidate();
            }
        }

        private Size size = new Size(14, 14);
        /// <summary>
        /// 按钮Size
        /// </summary>
        [Description("按钮Size")]
        [Category("杂项")]
        [PropertyOrder(-132)]
        [DefaultValue(typeof(Size), "14, 14")]
        public Size Size
        {
            get { return this.size; }
            set
            {
                if (this.size == value || value.Width < 0 || value.Height < 0)
                    return;

                this.size = value;
                this.InitializeInvalidate();
            }
        }

        private LRMargin lRMargin = new LRMargin(2, 2);
        /// <summary>
        /// 按钮左右外边距
        /// </summary>
        [Description("按钮左右外边距")]
        [Category("杂项")]
        [PropertyOrder(-130)]
        [DefaultValue(typeof(LRMargin), "2,2")]
        public LRMargin LRMargin
        {
            get { return this.lRMargin; }
            set
            {
                if (this.lRMargin == value || value.Left < 0 || value.Right < 0)
                    return;

                this.lRMargin = value;
                this.InitializeInvalidate();
            }
        }

        private bool checkButton = false;
        /// <summary>
        /// 按钮是否为check按钮
        /// </summary>
        [Description("按钮是否为check按钮")]
        [Category("杂项")]
        [PropertyOrder(-122)]
        [DefaultValue(false)]
        public bool CheckButton
        {
            get { return this.checkButton; }
            set
            {
                if (this.checkButton == value)
                    return;

                this.checkButton = value;
                this.Invalidate();
            }
        }

        private bool _checked = false;
        /// <summary>
        /// 按钮为check按钮时是否选中
        /// </summary>
        [Description("按钮为check按钮时是否选中")]
        [Category("杂项")]
        [PropertyOrder(-120)]
        [DefaultValue(false)]
        public bool Checked
        {
            get { return this._checked; }
            set
            {
                if (this._checked == value)
                    return;

                this._checked = value;

                if (this.owner != null && this.owner.Parent != null && this.owner.Parent is TabControlPlus)
                {
                    TabPagePlus tab_page = (TabPagePlus)this.owner;
                    this.OnTabItemCustomButtonCheckedChanged(new TabControlPlusTabItemCustomButttonOperatedEventArgs(tab_page, this));
                }

                this.Invalidate();
            }
        }

        private StyleAppearanceObject unCheckedStyleAppearance;
        /// <summary>
        /// 未选中状态样式
        /// </summary>
        [Description("未选中状态样式")]
        [Category("杂项")]
        [PropertyOrder(-118)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public StyleAppearanceObject UnCheckedStyleAppearance
        {
            get
            {
                if (this.unCheckedStyleAppearance == null)
                    this.unCheckedStyleAppearance = new StyleAppearanceObject(this.owner, null);
                return this.unCheckedStyleAppearance;
            }
        }

        private StyleAppearanceObject checkedStyleAppearance;
        /// <summary>
        /// 已选中状态样式
        /// </summary>
        [Description("已选中状态样式")]
        [Category("杂项")]
        [PropertyOrder(-116)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public StyleAppearanceObject CheckedStyleAppearance
        {
            get
            {
                if (this.checkedStyleAppearance == null)
                    this.checkedStyleAppearance = new StyleAppearanceObject(this.owner, null);
                return this.checkedStyleAppearance;
            }
        }

        private object tag = null;
        /// <summary>
        /// 自定义保存数据
        /// </summary>
        [Description("自定义保存数据")]
        [Category("杂项")]
        [PropertyOrder(-114)]
        [Bindable(true)]
        [Localizable(false)]
        [DefaultValue(null)]
        [TypeConverter(typeof(StringConverter))]
        public object Tag
        {
            get { return this.tag; }
            set { this.tag = value; }
        }

        private string tipText = "";
        /// <summary>
        /// 按钮提示文本
        /// </summary>
        [Description("按钮提示文本")]
        [Category("杂项")]
        [PropertyOrder(-112)]
        [DefaultValue("")]
        [Browsable(true)]
        public string TipText
        {
            get { return this.tipText; }
            set
            {
                if (this.tipText == value)
                    return;

                this.tipText = value;
            }
        }

        #region 用于临时存储运行信息

        /// <summary>
        /// 按钮rect（包含:外边距、内边距、内容）
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        protected internal Rectangle M_Rect { get; set; }

        /// <summary>
        /// 按钮rect（只包含:内容）
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        protected internal Rectangle C_Rect { get; set; }

        /// <summary>
        /// 自定义保存数据
        /// </summary>
        [DefaultValue(null)]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public object Data { get; set; }

        #endregion

        #endregion

        #region 字段

        private TabPagePlus owner;

        #endregion

        public TabControlPlusTabItemCustomButton()
        {

        }

        #region 虚方法

        internal protected virtual void OnTabItemCustomButtonClick(TabControlPlusTabItemCustomButttonOperatedEventArgs e)
        {
            if (this.tabItemCustomButtonClick != null && this.owner != null && this.owner.Parent != null && this.owner.Parent is TabControlPlus)
            {
                this.tabItemCustomButtonClick(this.owner.Parent, e);
            }
        }

        internal protected virtual void OnTabItemCustomButtonCheckedChanged(TabControlPlusTabItemCustomButttonOperatedEventArgs e)
        {
            if (this.tabItemCustomButtonCheckedChanged != null && this.owner != null && this.owner.Parent != null && this.owner.Parent is TabControlPlus)
            {
                this.tabItemCustomButtonCheckedChanged(this.owner.Parent, e);
            }
        }

        internal protected virtual void OnTabItemCusomButtonDrawAfter(TabControlPlusTabItemCustomButtonDrawAfterEventArgs e)
        {
            if (this.tabItemCusomButtonDrawAfter != null && this.owner != null && this.owner.Parent != null && this.owner.Parent is TabControlPlus)
            {
                Region source_c_region = e.Graphics.Clip;
                Region item_btn_c_region = source_c_region.Clone();//选项自定义按钮绘图区(C)
                item_btn_c_region.Intersect(e.ClipBounds_C);
                e.Graphics.Clip = item_btn_c_region;

                this.tabItemCusomButtonDrawAfter(this.owner.Parent, e);

                e.Graphics.Clip = source_c_region;
                item_btn_c_region.Dispose();
            }
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 设置自定义按钮所属的TabPage
        /// </summary>
        /// <param name="owner">设置自定义按钮所属的TabPage</param>
        internal void SetOwner(TabPagePlus owner)
        {
            this.owner = owner;
        }

        /// <summary>
        /// 控件重绘
        /// </summary>
        private void Invalidate()
        {
            if (this.owner != null && this.owner.Parent != null && this.owner.Parent is TabControlPlus)
            {
                TabControlPlus tabControl = (TabControlPlus)this.owner.Parent;
                tabControl.Invalidate();
            }
        }

        /// <summary>
        /// 控件重新初始化和重绘
        /// </summary>
        private void InitializeInvalidate()
        {
            if (this.owner != null && this.owner.Parent != null && this.owner.Parent is TabControlPlus)
            {
                TabControlPlus tabControl = (TabControlPlus)this.owner.Parent;
                tabControl.InitializeElement();
                tabControl.Invalidate();
            }
        }

        #endregion

        #region 外观

        public class StyleAppearanceObject : AppearanceObjectBase
        {
            internal StyleAppearanceObject(ISkinObject owner, AppearanceObjectBase parent)  : base(owner, parent) { }

            #region 属性

            private ItemAppearanceObject normal;
            /// <summary>
            /// 正常状态样式
            /// </summary>
            [Description("正常状态样式")]
            [PropertyOrder(-194)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
            public ItemAppearanceObject Normal
            {
                get
                {
                    if (this.normal == null)
                        this.normal = new ItemAppearanceObject(this.owner, this);
                    return this.normal;
                }
            }

            private ItemAppearanceObject enter;
            /// <summary>
            /// 鼠标进入状态样式
            /// </summary>
            [Description("鼠标进入状态样式")]
            [PropertyOrder(-192)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
            public ItemAppearanceObject Enter
            {
                get
                {
                    if (this.enter == null)
                        this.enter = new ItemAppearanceObject(this.owner, this);
                    return this.enter;
                }
            }

            #endregion

            public class ItemAppearanceObject : AppearanceObjectBase
            {
                internal ItemAppearanceObject(ISkinObject owner, AppearanceObjectBase parent)  : base(owner, parent) { }

                #region 重写

                /// <summary>
                /// 刷新控件
                /// </summary>
                public override void Invalidate()
                {
                    if (this.owner != null && ((TabPagePlus)this.owner).Parent != null && ((TabPagePlus)this.owner).Parent is TabControlPlus)
                    {
                        TabControlPlus tabControl = (TabControlPlus)((TabPagePlus)this.owner).Parent;
                        tabControl.Invalidate();
                    }
                }

                #endregion

                #region 属性

                private Image image = null;
                /// <summary>
                /// 图片
                /// </summary>
                [Description("图片")]
                [PropertyOrder(-154)]
                [DefaultValue(null)]
                [RefreshProperties(RefreshProperties.Repaint)]
                public Image Image
                {
                    get { return image; }
                    set
                    {
                        if (this.image == value)
                            return;

                        this.image = value;
                        this.Invalidate();
                    }
                }

                #endregion

            }

        }

        #endregion
    }

}
